package com.zyb.dfs.namenode.server;

import java.io.IOException;

/**
 * NameNode核心启动类
 *
 * @author zhonghuashishan
 */
public class NameNode {

    /**
     * NameNode是否在运行
     */
    private volatile Boolean shouldRun;
    /**
     * 负责管理元数据的核心组件，文件目录树
     */
    private FSNamesystem namesystem;

    /**
     * 负责管理集群中所有datanode的组件
     */
    private DataNodeManager dataNodeManager;

    /**
     * NameNode对外提供rpc接口的server，可以响应请求
     */
    private NameNodeRpcServer rpcServer;

    /**
     * 接受backupnode上传fsimage文件的server
     */
    private FSImageUploadServer fsimageUploadServer;

    public NameNode() {
        this.shouldRun = true;
    }

    /**
     * 初始化NameNode
     */
    private void initialize() throws Exception {
        this.dataNodeManager = new DataNodeManager();
        this.namesystem = new FSNamesystem(dataNodeManager);
        this.dataNodeManager.setNameSystem(namesystem);
        this.rpcServer = new NameNodeRpcServer(this.namesystem, this.dataNodeManager);
        this.fsimageUploadServer = new FSImageUploadServer();
    }

    /**
     * 让NameNode运行起来
     */
    private void start() throws Exception {
        this.fsimageUploadServer.start();
        this.rpcServer.start();
        this.rpcServer.blockUntilShutdown();
    }

    public static void main(String[] args) throws Exception {
        NameNode namenode = new NameNode();
        namenode.initialize();
        namenode.start();
    }

}
